package cn.tedu.tmall.front.mall.controller;

import cn.tedu.tmall.common.pojo.CurrentUser;
import cn.tedu.tmall.common.web.JsonResult;
import cn.tedu.tmall.front.mall.pojo.vo.CartVO;
import cn.tedu.tmall.front.mall.service.ICartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

@Api(tags = "购物车服务")
@Validated
@Slf4j
@RequestMapping("/cart")
@RestController
public class CartController {

    @Autowired
    private ICartService cartService;

    @PostMapping("/add")
    @ApiOperation("增加商品")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "goodsId",value = "商品id",required = true,dataType = "Long"),
            @ApiImplicitParam(name = "goodsNum",value = "商品数量",required = true,dataType = "Integer"),
    })
    public JsonResult add(@ApiIgnore @AuthenticationPrincipal CurrentUser user,
                          @Range(min = 1,message = "请输入合法的商品id") Long goodsId,
                          @Range(min = 1,max = 100,message = "商品数量必须是1~100之间!") Integer goodsNum){
        log.debug("增加商品-入参 用户id:{},商品id:{},商品数量:{}",user.getId(),goodsId,goodsNum);
        cartService.add(user,goodsId,goodsNum);
        return JsonResult.ok();
    }

    /**
     * 删除购物车商品
     * @param user
     * @return
     */
    @PostMapping("del")
    @ApiOperation("删除购物车商品")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "goodsId",value = "商品id",required = true,dataType = "Long")
    })
    // @RequestParam
    public JsonResult del(@ApiIgnore @AuthenticationPrincipal CurrentUser user,
                          @RequestParam @Range(min = 1,message = "请输入合法的商品id") Long goodsId){
        log.debug("我的购物车-入参 用户id:{}",user.getId());
        cartService.del(user,goodsId);
        return JsonResult.ok();
    }

    /**
     * 我的购物车 购物车的商品列表
     * @param user
     * @return
     */
    @PostMapping("list")
    @ApiOperation("我的购物车")
    public JsonResult list(@ApiIgnore @AuthenticationPrincipal CurrentUser user){
        log.debug("我的购物车-入参 用户id:{}",user.getId());
        List<CartVO> cartVOList = cartService.list(user);
        return JsonResult.ok(cartVOList);
    }




    /**
     * 修改商品数量
     * 修改购物车里面的已经有的商品的数量 直接修改 替换值 10 --> 99
     * @param user
     * @param goodsId
     * @param goodsNum
     * @return
     */
    @PostMapping("/modify")
    @ApiOperation("修改商品数量")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "goodsId",value = "商品id",required = true,dataType = "Long"),
            @ApiImplicitParam(name = "goodsNum",value = "商品数量",required = true,dataType = "Integer"),
    })
    public JsonResult modify(@ApiIgnore @AuthenticationPrincipal CurrentUser user,
                             @Range(min = 1,message = "请输入合法的商品id") Long goodsId,
                             @Range(min = 1,max = 100,message = "商品数量必须是1~100之间!") Integer goodsNum){
        log.debug("增加商品-入参 用户id:{},商品id:{},商品数量:{}",user.getId(),goodsId,goodsNum);
        cartService.modify(user,goodsId,goodsNum);
        return JsonResult.ok();
    }

    /**
     * 增加
     * 修改购物车里面的已经有的商品的数量   加 加一个,加多个 减  减一个 减多个
     * @param user
     * @param goodsId
     * @param goodsNum
     * @return
     */
    @PostMapping("/incr")
    @ApiOperation("增加一个或者多个商品")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "goodsId",value = "商品id",required = true,dataType = "Long"),
            @ApiImplicitParam(name = "goodsNum",value = "商品数量",required = true,dataType = "Integer"),
    })
    public JsonResult incr(@ApiIgnore @AuthenticationPrincipal CurrentUser user,
                           @Range(min = 1,message = "请输入合法的商品id") Long goodsId,
                           @Range(min = 1,max = 100,message = "商品数量必须是1~100之间!") Integer goodsNum){
        log.debug("增加商品-入参 用户id:{},商品id:{},商品数量:{}",user.getId(),goodsId,goodsNum);
        cartService.incr(user,goodsId,goodsNum);
        return JsonResult.ok();
    }

    /**
     * 增加
     * 修改购物车里面的已经有的商品的数量   加 加一个,加多个 减  减一个 减多个
     * @param user
     * @param goodsId
     * @param goodsNum
     * @return
     */
    @PostMapping("/reduce")
    @ApiOperation("减少一个或者多个商品")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "goodsId",value = "商品id",required = true,dataType = "Long"),
            @ApiImplicitParam(name = "goodsNum",value = "商品数量",required = true,dataType = "Integer"),
    })
    public JsonResult reduce(@ApiIgnore @AuthenticationPrincipal CurrentUser user,
                             @Range(min = 1,message = "请输入合法的商品id") Long goodsId,
                             @Range(min = 1,max = 100,message = "商品数量必须是1~100之间!") Integer goodsNum){
        log.debug("增加商品-入参 用户id:{},商品id:{},商品数量:{}",user.getId(),goodsId,goodsNum);
        cartService.reduce(user,goodsId,goodsNum);
        return JsonResult.ok();
    }


    /**
     * 选中购物车中的商品
     * @param user
     * @param goodsId
     * @return
     */
    @PostMapping("/pick/on")
    @ApiOperation("选中商品")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "goodsId",value = "商品id",required = true,dataType = "Long")
    })
    public JsonResult pick(@ApiIgnore @AuthenticationPrincipal CurrentUser user,
                           @RequestParam @Range(min = 1,message = "请输入合法的商品id") Long goodsId){
        log.debug("选中商品-入参 用户id:{},商品id:{}",user.getId(),goodsId);
        cartService.pick(user,goodsId);
        return JsonResult.ok();
    }


    /**
     * 选中购物车中的商品
     * @param user
     * @param goodsId
     * @return
     */
    @PostMapping("/pick/off")
    @ApiOperation("取消选中商品")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "goodsId",value = "商品id",required = true,dataType = "Long")
    })
    public JsonResult pickOff(@ApiIgnore @AuthenticationPrincipal CurrentUser user,
                              @RequestParam @Range(min = 1,message = "请输入合法的商品id") Long goodsId){
        log.debug("选中商品-入参 用户id:{},商品id:{}",user.getId(),goodsId);
        cartService.unPick(user,goodsId);
        return JsonResult.ok();
    }
    // 选中/取消选中  修改数量是一样
    //todo 1
    //接口1  选中  入参 用户信息,商品id    出参 五
    // cacheRepository
    //  大key mall_cart_用户id
    //  小key 商品id_checked
    //  更新  商品id_checked = 1
    //  hashOperations.put(cartKey,goodsNumHashKey,1);
    //接口2  取消选中 入参 用户信息 商品id 出参 无  todo 2
    // cacheRepository
    //  大key mall_cart_用户id
    //  小key 商品id_checked
    //  更新  商品id_checked = 0
    //          String cartKey = getCartKey(userId);
    //        //商品id_num
    //        String goodsNumHashKey = getGoodsCheckedHashKey(goodsId);
    //        HashOperations<String, String, Object> hashOperations = redisTemplate.opsForHash();
    //        //put 操作 没有就添加 有就覆盖 ,选中,取消选中可以采用同一种方法,但是需要注意小key不同
    //        hashOperations.put(cartKey,goodsNumHashKey,0);

    //todo 3
    //增加商品时
    // 判断购物车是否有当前商品
    //   没有 直接增加
    //   有   商品数量 = 原来的数量 + 新加的商品数量
}
